package eiisan.forkjointest.recursiveaction;

import java.util.concurrent.RecursiveAction;

public class IncrementTask extends RecursiveAction {
    static final int THRESHOLD = 1000;
    final long[] array;
    final int lo, hi;

    public IncrementTask(long[] array, int lo, int hi) {
        this.array = array;
        this.lo = lo;
        this.hi = hi;
    }

    public IncrementTask(long[] array) {
        this(array, 0, array.length);
    }

    protected void compute() {
        if (hi - lo < THRESHOLD) {
            for (int i = lo; i < hi; ++i) {
                System.out.println(Thread.currentThread().getName() + " ===> " + i);
                array[i]++;
            }
        } else {
            int mid = (lo + hi) >>> 1;
            System.out.println(Thread.currentThread().getName() + " invokeAll ===> " + lo + ", " + hi);
            invokeAll(new IncrementTask(array, lo, mid),
                    new IncrementTask(array, mid, hi));
        }
    }
}
